package br.cns.metrics;

import br.cns.Metric;
import br.cns.TMetric;
import br.cns.transformations.FloydWarshallShortestPath;

public class LinkEfficiency implements Metric {

	private static final LinkEfficiency instance = new LinkEfficiency();

	private LinkEfficiency() {
	}

	public static LinkEfficiency getInstance() {
		return instance;
	}

	@Override
	public double calculate(int[][] matrix) {
		int[][] shortestPath = FloydWarshallShortestPath.getInstance().transform(matrix);
		int n = matrix.length;
		int m = 0;
		int sum = 0;
		int a = 0;
		for (int i = 0; i < n; i++){
			for (int j = i+1; j < n; j++){
				if (matrix[i][j] != 0){
					a++;
				}
			}	
		}
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				if (shortestPath[i][j] != 0){
					sum += shortestPath[i][j];
					m++;
				}
			}	
		}
		
		return 1 - (sum/(m * 1.0))/a;
	}

	@Override
	public String name() {
		return TMetric.LINK_EFFICIENCY.toString();
	}

}
